import { useState, useEffect } from 'react';

// 搜索项类型定义
export interface SearchItem {
  id: number;
  title: string;
  type: 'review' | 'guide' | 'article';
  url: string;
  description: string;
  category: string;
}

// 模拟的搜索数据 - 在实际应用中，这些数据可能来自API或本地存储
export const searchData: SearchItem[] = [
  {
    id: 1,
    title: 'TheraGun Pro: The Ultimate Recovery Tool?',
    type: 'review',
    url: '/reviews/theragun-pro',
    description: 'We tested the TheraGun Pro for 3 months to see if it lives up to the hype.',
    category: 'Massage Guns'
  },
  {
    id: 2,
    title: 'Hyperice Hypervolt Plus Review',
    type: 'review',
    url: '/reviews/hypervolt-plus',
    description: 'An in-depth look at the Hyperice Hypervolt Plus massage gun.',
    category: 'Massage Guns'
  },
  {
    id: 3,
    title: 'Best Wellness Products of 2024',
    type: 'guide',
    url: '/guides/best-wellness-2024',
    description: 'Our comprehensive guide to the top wellness products this year.',
    category: 'Guides'
  },
  {
    id: 4,
    title: 'How to Choose the Right Massage Gun',
    type: 'guide',
    url: '/guides/massage-gun-buying-guide',
    description: 'Everything you need to know before buying a massage gun.',
    category: 'Guides'
  },
  {
    id: 5,
    title: 'Recovery and Wellness Tips',
    type: 'article',
    url: '/guides/recovery-tips',
    description: 'Expert tips for optimizing your recovery routine.',
    category: 'Health & Wellness'
  },
  {
    id: 6,
    title: 'Foam Roller vs Massage Gun: Which is Better?',
    type: 'article',
    url: '/guides/foam-roller-vs-massage-gun',
    description: 'A detailed comparison of foam rollers and massage guns for recovery.',
    category: 'Comparisons'
  },
  {
    id: 7,
    title: 'Therabody PowerDot 2.0 Review',
    type: 'review',
    url: '/reviews/powerdot-2',
    description: 'Testing the PowerDot 2.0 wireless muscle stimulator for recovery.',
    category: 'TENS Units'
  },
  {
    id: 8,
    title: 'Best Sleep Tracking Devices 2024',
    type: 'guide',
    url: '/guides/sleep-tracking-devices',
    description: 'The top sleep tracking devices to improve your sleep quality.',
    category: 'Sleep & Recovery'
  }
];

// 搜索Hook
export function useSearch() {
  const [searchQuery, setSearchQuery] = useState('');
  const [searchResults, setSearchResults] = useState<SearchItem[]>([]);
  const [isLoading, setIsLoading] = useState(false);

  useEffect(() => {
    if (searchQuery.trim() === '') {
      setSearchResults([]);
      return;
    }

    setIsLoading(true);

    // 模拟API延迟
    const timeoutId = setTimeout(() => {
      const filtered = searchData.filter(item =>
        item.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
        item.description.toLowerCase().includes(searchQuery.toLowerCase()) ||
        item.category.toLowerCase().includes(searchQuery.toLowerCase())
      );
      
      setSearchResults(filtered);
      setIsLoading(false);
    }, 150);

    return () => {
      clearTimeout(timeoutId);
      setIsLoading(false);
    };
  }, [searchQuery]);

  return {
    searchQuery,
    setSearchQuery,
    searchResults,
    isLoading
  };
}

// 高级搜索功能
export function searchByCategory(category: string): SearchItem[] {
  return searchData.filter(item => 
    item.category.toLowerCase().includes(category.toLowerCase())
  );
}

export function searchByType(type: 'review' | 'guide' | 'article'): SearchItem[] {
  return searchData.filter(item => item.type === type);
}

// 获取热门搜索词
export function getPopularSearchTerms(): string[] {
  return [
    'massage gun',
    'theragun',
    'recovery',
    'wellness',
    'sleep tracking',
    'TENS unit',
    'foam roller'
  ];
}
